﻿#include "Armadillo.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    Armadillo::Armadillo(MonsterInfo *info) : DigOutZombie(info)
    {
    }

    int Armadillo::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        if (_runAway && getEnvir()->Random->Next(4) == 0)
        {
            _runAway = false;
        }

        return DigOutZombie::Attacked(attacker, damage, type, damageWeapon);
    }

    int Armadillo::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        if (_runAway && getEnvir()->Random->Next(4) == 0)
        {
            _runAway = false;
        }

        return DigOutZombie::Attacked(attacker, damage, type);
    }

    void Armadillo::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        switch (getEnvir()->Random->Next(0, 6))
        {
            case 0:
            {
                    Retreat();
            }
                break;
            case 1:
            {
                    S::ObjectAttack *tempVar = new S::ObjectAttack();
                    tempVar->ObjectID = ObjectID;
                    tempVar->Direction = getDirection();
                    tempVar->Location = getCurrentLocation();
                    tempVar->Type = 1;
                    Broadcast(tempVar);
                    int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                    if (damage == 0)
                    {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                        return;
                    }

                    DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 400, {getTarget(), damage / 2, DefenceType::ACAgility});
                    ActionList.push_back(action);

                    damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                    if (damage == 0)
                    {
//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                        return;
                    }

                    action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 600, {getTarget(), damage / 2, DefenceType::ACAgility});
                    ActionList.push_back(action);

                    damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                    if (damage == 0)
                    {
//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                        return;
                    }

                    action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 800, {getTarget(), damage / 2, DefenceType::ACAgility});
                    ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
                break;
            default:
            {
                    S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                    tempVar2->ObjectID = ObjectID;
                    tempVar2->Direction = getDirection();
                    tempVar2->Location = getCurrentLocation();
                    Broadcast(tempVar2);
                    int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                    if (damage == 0)
                    {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                        return;
                    }

                    DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 400, {getTarget(), damage, DefenceType::ACAgility});
                    ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
                break;
        }
    }

    void Armadillo::Retreat()
    {
        MirDirection jumpDir = Functions::ReverseDirection(getDirection());

        Point *location;

        for (int i = 0; i < 2; i++)
        {
            location = Functions::PointMove(getCurrentLocation(), jumpDir, 1);
            if (!getCurrentMap()->ValidPoint(location))
            {
                return;
            }
        }

        for (int i = 0; i < 2; i++)
        {
            location = Functions::PointMove(getCurrentLocation(), jumpDir, 1);

            getCurrentMap()->GetCell(getCurrentLocation())->Remove(this);
            RemoveObjects(jumpDir, 1);
            setCurrentLocation(location);
            getCurrentMap()->GetCell(getCurrentLocation())->Add(this);
            AddObjects(jumpDir, 1);
        }

        S::ObjectBackStep *tempVar = new S::ObjectBackStep();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = location;
        tempVar->Distance = 2;
        Broadcast(tempVar);

        int damage = Stats[Stat::MaxDC];
        if (damage == 0)
        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 900, {getTarget(), damage, DefenceType::AC});
        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void Armadillo::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        auto targets = FindAllTargets(2, getCurrentLocation());

        for (int i = 0; i < targets.size(); i++)
        {
            if (targets[i]->Attacked(this, damage, defence) <= 0)
            {
                _runAway = true;
            };
        }
    }

    void Armadillo::ProcessTarget()
    {
        if (_runAway)
        {
            if (!getCanMove() || getTarget() == nullptr)
            {
                return;
            }

            MirDirection dir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());

            if (Walk(dir))
            {
                return;
            }

            switch (getEnvir()->Random->Next(2)) //No favour
            {
                case 0:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::NextDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
                default:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::PreviousDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
            }
        }
        else
        {
            DigOutZombie::ProcessTarget();
        }
    }
}
